package com.jmcgg.freesky.common.util;

import java.io.File;

import org.jconfig.Configuration;
import org.jconfig.ConfigurationManager;
import org.jconfig.handler.XMLFileHandler;

public class ConfigUtil extends FreeskyUtil {

	public static Configuration config = ConfigurationManager.getConfiguration(Const.FREESKY);

	public static void init(String configName) {
		config = ConfigurationManager.getConfiguration(configName);
	}

	public static Configuration getConfig() {
		return config;
	}

	public static String getProperty(String category, String name) {
		return config.getProperty(name, "", category);
	}

	public static boolean getBoolean(String propName) {
		return config.getBooleanProperty(propName, false);
	}

	public static String getProperty(String name) {
		return config.getProperty(name);
	}

	public static int getInt(String name, int defaultValue) {
		return config.getIntProperty(name, defaultValue);
	}

	public static void setProperty(String name, String value, String categoryName) {
		config.setProperty(name, value, categoryName);
	}

	public static void setProperty(String name, String value) {
		config.setProperty(name, value);
	}

	public void saveTest() {
		ConfigurationManager cm = ConfigurationManager.getInstance();
		File file = new File("test.xml");
		XMLFileHandler handler = new XMLFileHandler();
		handler.setFile(file);
		try {
			System.out.println("trying to load file");
			cm.load(handler, "myConfig");
			System.out.println("file successfully processed");
			Configuration config = ConfigurationManager.getConfiguration("myConfig");
			config.setProperty("TEST", "Added property");
			System.out.println("trying to save file");
			cm.save(handler, config);
			System.out.println("file successfully saved");
			System.out.println("TEST:" + config.getProperty("TEST"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
